package com.ydj.app.protocol.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.lang.StringUtils;

import java.util.List;


@Getter
@Setter
@Accessors(chain = true)
public class ForecastQaItemDto {
    private String label;
    // 暂时定义了checkbox 和 text
    private Type type;
    private String placeholder;
    private Boolean required;
    private String value;
    private List<QaOptionsItem> options;

    @Getter
    @AllArgsConstructor
    public enum Type {
        CheckBox("checkBox"),
        InputBox("inputBox");

        String value;
    }

    @Getter
    @Setter
    @Accessors(chain = true)
    public static class QaOptionsItem {
        // 题目
        private String label;
        // checkbox是否勾选上
        private Boolean checked;
        // 输入框（可选）
        private QaOptionsItemInputBox inputBox;

        public QaOptionsItem(String label) {
            this.label = label;
        }

        public QaOptionsItem(String label, String textBoxPlaceholder) {
            this.label = label;
            this.inputBox = new QaOptionsItemInputBox().setPlaceholder(textBoxPlaceholder);
        }

        @Getter
        @Setter
        @Accessors(chain = true)
        public static class QaOptionsItemInputBox {
            private String placeholder;
            // 用户输入的内容
            private String value;
        }
    }

    public String getExcelQuestion() {
        return getLabel();
    }

    public String getExcelAnswer() {
        if (type == Type.InputBox) {
            return value;
        } else if (type == Type.CheckBox) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < options.size(); i++) {
                QaOptionsItem item = options.get(i);
                if (item.getChecked()) {
                    sb.append(item.getLabel());
                    if (item.getInputBox() != null && StringUtils.isNotBlank(item.getInputBox().getValue())) {
                        sb.append(item.getInputBox().getValue());
                    }
                }
            }
            return sb.toString();
        }
        return "";
    }

    public static String toQuestionPrompt(List<ForecastQaItemDto> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            ForecastQaItemDto item = list.get(i);
            sb.append(i+1).append(". ");
            if (item.getType() == Type.InputBox) {
                sb.append(item.getLabel()).append("\n");
            } else if (item.getType() == Type.CheckBox) {
                sb.append(item.getLabel()).append("\n");
                for (int j = 0; j < item.getOptions().size(); j++) {
                    QaOptionsItem optionsItem = item.getOptions().get(j);
                    sb.append("\t")
                            .append((char) ('A' + j))
                            .append(". ")
                            .append(optionsItem.getLabel()).append("\n");
                }
            }
        }
        return sb.toString();
    }
    public static String toAnswerPrompt(List<ForecastQaItemDto> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            ForecastQaItemDto item = list.get(i);
            sb.append(i+1).append(". ");
            if (item.getType() == Type.InputBox) {
                if (StringUtils.isNotBlank(item.getValue())) {
                    sb.append(item.getValue());
                } else {
                    sb.append("用户没有填写");
                }
                sb.append("\n");
            } else if (item.getType() == Type.CheckBox) {
                sb.append("\n");
                List<QaOptionsItem> itemOptions = item.getOptions();
                for (int j = 0; j < itemOptions.size(); j++) {
                    QaOptionsItem option = itemOptions.get(j);
                    sb.append("\t");
                    sb.append(option.getChecked() ? "【选中】" : "【未选中】")
                            .append((char) ('A' + j))
                            .append(".")
                            .append(option.getLabel())
                            .append("\n");
                    if (option.getInputBox() != null && StringUtils.isNotBlank(option.getInputBox().getValue())) {
                        sb.append("\t\t【补充填写内容】")
                                .append(option.getInputBox().getValue())
                                .append("\n");
                    }
                }
            }
        }
        return sb.toString();
    }

     /*
    这里是json的demo
[

	{
		label: 题目
		type: 'checkBox'
		placeholder: 提示内容
		required: true,
		options: [
			{
				label: 选项里的
				checked: true
				inputBox:
				{
					placeholder: 提示内容
					value: 文本框填写的内容. 作为题目没有该项	，答案才有
				}
			}，
			。。。。。
		]
	},


	{
		label: 题目
		type: 'inputBox'
		placeholder: 提示内容
		required: true,
		value: 文本框填写的内容. 作为题目没有该项	，答案才有
	}

]


     */
}

